package com.shop.flowershopmanager.controller;


import com.shop.flowershopmanager.entity.SalesRecord;
import com.shop.flowershopmanager.service.SalesRecordService;
import com.shop.flowershopmanager.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@RestController
@Slf4j
public class SalesRecordServiceController {
    @Resource
    private SalesRecordService salesRecordService;

    @GetMapping("/v1/getSalesRecord")
    public R<Object> querySalesRecord(Integer sId) {
        return R.ok(salesRecordService.querySalesRecord(sId));
    }

    @PostMapping("v1/addSalesRecord")
    public R<Object> addSalesRecord(HttpServletRequest request) throws ParseException {
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        DateTimeFormatter dtf4 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        Date date = fmt.parse(request.getParameter("sDate"));
        int fId = Integer.parseInt(request.getParameter("fId"));
        int count = Integer.parseInt(request.getParameter("count"));
        int total = Integer.parseInt(request.getParameter("total"));
        String client = request.getParameter("client");
        String remark = request.getParameter("remark");
        String emp = request.getParameter("emp");
        ZoneId defaultZoneId = ZoneId.systemDefault();
        LocalDate createLocal = LocalDate.now();
        Date createdAt = Date.from(createLocal.atStartOfDay(defaultZoneId).toInstant());
        Date updateCreate = Date.from(createLocal.atStartOfDay(defaultZoneId).toInstant());
        SalesRecord salesRecord = new SalesRecord(date, fId, count, total, client, remark, emp, createdAt, updateCreate);
        int index = salesRecordService.insertSalesRecord(salesRecord);
        if (index > 0) {
            return R.ok("添加成功");
        } else {
            return R.failed("添加失败");
        }
    }
    @PutMapping("v1/updateSalesRecord")
    public R<Object> updateSalesRecord(HttpServletRequest request)throws ParseException{
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        DateTimeFormatter dtf4 = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
        Date date = fmt.parse(request.getParameter("sDate"));
        int sId = Integer.parseInt(request.getParameter("sId"));
        int fId = Integer.parseInt(request.getParameter("fId"));
        int count = Integer.parseInt(request.getParameter("count"));
        int total = Integer.parseInt(request.getParameter("total"));
        String client = request.getParameter("client");
        String remark = request.getParameter("remark");
        String emp = request.getParameter("emp");
        ZoneId defaultZoneId = ZoneId.systemDefault();
        LocalDate createLocal = LocalDate.now();
        Date createdAt = Date.from(createLocal.atStartOfDay(defaultZoneId).toInstant());
        Date updateCreate = Date.from(createLocal.atStartOfDay(defaultZoneId).toInstant());
        SalesRecord salesRecord = new SalesRecord(sId,date, fId, count, total, client, remark, emp, createdAt, updateCreate);
        int index = salesRecordService.updateSalesRecord(salesRecord);
        if (index > 0) {
            return R.ok("修改成功");
        } else {
            return R.failed("修改失败");
        }
    }

    @DeleteMapping("v1/deleteSalesRecord")
    public R<Object> deleteSalesRecord(Integer sId){
       int i = salesRecordService.deleteSalesRecordById(sId);
       if (i>0){
           return R.ok("删除成功");
       }else {
           return  R.failed("删除失败");
       }
    }
}
